#include <stdlib.h>
#include <string.h>
#include <stdio.h>

typedef struct tagListNode {
    int val;
    struct tagListNode *next;
} ListNode;
typedef struct {
    ListNode *listHead;
    ListNode *listTail;
} NodeList;

int dfs(NodeList *map, int val, int *restricted, int father) {
    int res = 0;
    NodeList *allNode = &map[val];
    ListNode *head = allNode->listHead;
    while (head) {
        int tmp = head->val;
        if (tmp != father && restricted[tmp] != -1) {
            res++;
            res += dfs(map, tmp, restricted, val);
        }
        head = head->next;
    }
    return res;
}

int reachableNodes(int n, int **edges, int edgesSize, int *edgesColSize, int *restricted, int restrictedSize) {
    NodeList *map = calloc(n, sizeof(NodeList));
    for (int i = 0; i < edgesSize; ++i) {
        int a = edges[i][0];
        int b = edges[i][1];
        NodeList *tmpA = &map[a];
        ListNode *tmpNodeB = calloc(1, sizeof(ListNode));
        tmpNodeB->val = b;
        tmpNodeB->next = NULL;
        if (tmpA->listHead == NULL) {
            tmpA->listHead = tmpNodeB;
        } else {
            tmpA->listTail->next = tmpNodeB;
        }
        tmpA->listTail = tmpNodeB;
        NodeList *tmpB = &map[b];
        ListNode *tmpNodeA = calloc(1, sizeof (ListNode));
        tmpNodeA->val = a;
        tmpNodeA->next = NULL;
        if (tmpB->listHead == NULL) {
            tmpB->listHead = tmpNodeA;
        } else {
            tmpB->listTail->next= tmpNodeA;
        }
        tmpB->listTail = tmpNodeA;
    }
    int *restrictRct = calloc(n, sizeof (int));
    for (int i = 0; i < restrictedSize; ++i) {
        restrictRct[restricted[i]] = 1;
    }
    return dfs(map, 0, restrictRct, -1) + 1;
}

int main() {
    int n = 7, edges[][2] = {{0, 1},
                             {1, 2},
                             {3, 1},
                             {4, 0},
                             {0, 5},
                             {5, 6}}, restricted[2] = {4, 5};
    int *edge[6];
    for (int i = 0; i < 6; ++i) {
        edge[i] = edges[i];
    }
    int edgesColSize = 2;
    int ret = reachableNodes(n, edge, 6, &edgesColSize, restricted, 2);
    printf("%d", ret);
}